package com.zbkj.service.service.adminregion.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zbkj.common.model.bill.Bill;
import com.zbkj.common.model.bill.PlatformDailyStatement;
import com.zbkj.common.model.merchant.Merchant;
import com.zbkj.common.model.user.User;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.region.SearchRegionVo;
import com.zbkj.common.request.FundsFlowRequest;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.response.FundsFlowResponse;
import com.zbkj.common.response.PlatformStatementResponse;
import com.zbkj.common.utils.CrmebDateUtil;
import com.zbkj.common.vo.DateLimitUtilVo;
import com.zbkj.service.dao.BillDao;
import com.zbkj.service.helper.region.RegionHelper;
import com.zbkj.service.service.BillService;
import com.zbkj.service.service.MerchantService;
import com.zbkj.service.service.UserService;
import com.zbkj.service.service.adminregion.IRegionFinanceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName: FinanceServiceImpl
 * @Description: TODO
 * @Author: fanbing
 * @Date: 2024/8/18 11:25
 * @Version: V1.0
 **/
@Service
@Slf4j
public class IRegionFinanceServiceImpl implements IRegionFinanceService {


    @Autowired
    private UserService userService;
    @Autowired
    private MerchantService merchantService;
    @Autowired
    private RegionHelper regionHelper;

    @Resource
    private BillDao dao;

    @Override
    public PageInfo<FundsFlowResponse> getFundsFlow(FundsFlowRequest request, PageParamRequest pageParamRequest) {
        SearchRegionVo currentUserRegion = regionHelper.getCurrentUserRegion();
        if (currentUserRegion.isMerchantIdNullFlag()) {
            return new PageInfo();
        }

        PageInfo<Bill> billPageInfo = this.getFlow(request, pageParamRequest, currentUserRegion.getMerchantIds());
        List<Bill> billList = billPageInfo.getList();
        if (CollUtil.isEmpty(billList)) {
            return CommonPage.copyPageInfo(billPageInfo, CollUtil.newArrayList());
        }
        List<Integer> uidList = billList.stream().filter(e -> e.getUid() > 0).map(Bill::getUid).collect(Collectors.toList());
        Map<Integer, User> userHashMap = CollUtil.newHashMap();
        if (CollUtil.isNotEmpty(uidList)) {
            userHashMap = userService.getUidMapList(uidList);
        }
        List<Integer> merIdList = billList.stream().filter(e -> e.getMerId() > 0).map(Bill::getMerId).collect(Collectors.toList());
        Map<Integer, Merchant> merchantMap = CollUtil.newHashMap();
        if (CollUtil.isNotEmpty(merIdList)) {
            merchantMap = merchantService.getMapByIdList(merIdList);
        }
        List<FundsFlowResponse> responseList = CollUtil.newArrayList();
        for (Bill bill : billList) {
            FundsFlowResponse response = new FundsFlowResponse();
            BeanUtils.copyProperties(bill, response);
            response.setNickName(bill.getUid() > 0 ? userHashMap.get(bill.getUid()).getNickname() : "");
            response.setMerName(bill.getMerId() > 0 ? merchantMap.get(bill.getMerId()).getName() : "");
            responseList.add(response);
        }
        return CommonPage.copyPageInfo(billPageInfo, responseList);
    }


    private PageInfo<Bill> getFlow(FundsFlowRequest request, PageParamRequest pageParamRequest, List<Integer> merchantIds) {
        Page<Bill> page = PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        LambdaQueryWrapper<Bill> lqw = Wrappers.lambdaQuery();
        lqw.in(Bill::getMerId, merchantIds);
        if (StrUtil.isNotBlank(request.getOrderNo())) {
            lqw.eq(Bill::getOrderNo, request.getOrderNo());
        }
        if (StrUtil.isNotBlank(request.getDateLimit())) {
            DateLimitUtilVo dateLimit = CrmebDateUtil.getDateLimit(request.getDateLimit());
            lqw.between(Bill::getCreateTime, dateLimit.getStartTime(), dateLimit.getEndTime());
        }
        lqw.orderByDesc(Bill::getId);
        List<Bill> billList = dao.selectList(lqw);
        return CommonPage.copyPageInfo(page, billList);
    }


}
